<template>
  <div class="acBox">
    <div class="acMap">
      <div class="search">
        <img src="@/assets/image/searIcon.png" />
        <input
          type="text"
          placeholder="请输入查询的省份"
          class="search"
          v-model.l.lazy="search"
        />
      </div>
      <div class="chinaMap" ref="map_ref"></div>
      <div id="toolbox">
        <!--v-blind,:只能绑定属性-->
        <div class="introduction">2022 {{ province }}</div>
        <div class="introduction">活动总数量：120</div>
        <div class="introduction">活动总支出：121732元</div>
        <div class="introduction">活动总收入：131656元</div>
      </div>
    </div>
    <div v-show="listed
">
      <div class="acList">
        <div class="listTitle">团队活动列表<span style="position: absolute;right: 1rem;cursor: pointer;" @click="listed=false">X</span></div>
        <div class="listBox">
          <table>
            <thead>
            <tr>
              <td>活动名称</td>
              <td>活动支出（元）</td>
              <td>活动类别</td>
              <td>活动时间</td>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(ac, index) in aclist" :key="ac">
              <template v-if="index < 4">
                <td>{{ ac.acName }}</td>
                <td>{{ ac.pay }}</td>
                <td>{{ actype[ac.type] }}</td>
                <td>{{ ac.createTime.slice(0, 10) }}</td>
              </template>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>
</template>
<script>
// import { mapState } from 'vuex'
import MAP from '../../public/static/map/china.json'
import axios from 'axios'
import { getProvinceMapInfo } from '@/assets/utils/map_utils'
export default {
  data() {
    return {
      search: null,
      listed:false,
      chartInstance: null,
      allData: null,
      mapData: {}, // 所获取的省份的地图矢量数据
      aclist: [],
      actype: ['家庭', '学校', '社区']
    }
  },
  created() {},
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    // this.getData()
  },
  unmounted() {
    window.removeEventListener('resize', this.screenAdapter)
  },
  watch: {
    search(newValue, oldValue) {
      this.queryRegion(newValue, oldValue)
      if (
        newValue == '' ||
        newValue.length < 2 ||
        newValue.length > 3 ||
        newValue === '内蒙' ||
        newValue === '黑龙'
      ) {
        console.log(newValue.length)
        document.getElementById('toolbox').style.display = 'none'
        this.initChart()
        console.log(newValue)
      } else {
        //this.search(newValue, oldValue)
        this.listed = true
        this.map(newValue, oldValue)
      }
    }
  },
  methods: {
    async queryRegion(newValue, oldValue) {
      console.log(newValue, oldValue)
      if (newValue != '') {
        const { data: res } = await this.$http.get(
          'infoservice/activity/queryByRegion',
          { params: { region: newValue } }
        )
        console.log(res.data.list)
        this.aclist = res.data.list
      } else {
        const { data: res } = await this.$http.get(
          'infoservice/activity/queryAll'
        )
        this.aclist = res.data.list
      }
    },
    initChart() {
      this.chartInstance = this.$ECharts.init(this.$refs.map_ref, 'chalk')
      // 获取中国地图的矢量数据
      // http://localhost:8999/static/map/china.json
      // 由于我们现在获取的地图矢量数据并不是位于KOA2的后台, 所以咱们不能使用this.$http
      const ret = MAP
      // const ret = await axios.get('http://localhost:8080/static/map/china.json')
      let geoCoordMap = [
        { name: '广东', value: [113.5107, 23.2196] },
        { name: '贵州', value: [109.26433, 27.81621] },
        { name: '湖南', value: [112.5439, 27.7075] },
        { name: '湖北', value: [114.3896, 30.6628] },
        { name: '福建', value: [119.27345, 26.04769] },
        { name: '河北', value: [115.45875, 38.87757] },
        { name: '内蒙古', value: [111.62299, 40.80772] },
        { name: '辽宁', value: [123.1238, 42.1216] }
      ]

      // 中国地图线路特效配置 end
      this.$ECharts.registerMap('china', ret)
      const initOption = {
        title: {
          //text: '▎ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: 'rgba(255,255,255,0.2)',
            borderColor: '#333'
          },
          label: {
            normal: {
              //show: true,
              textStyle: {
                fontSize: 15,
                fontWeight: 'bold',
                color: '#fff'
              }
            }
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        },
        series: [
          {
            symbol: 'circle',
            rippleEffect: {
              scale: 5,
              brushType: 'stroke'
            },
            tooltip: {
              trigger: 'item',
              show: false
            },
            // name: '省市',
            legend: {
              textStyle: {
                color: '#fff',
                fontSize: 7
              }
            },
            color: 'rgb(217, 142, 0)',
            type: 'effectScatter',
            label: {
              normal: {
                show: true,
                position: 'top',
                formatter: '{b}',
                textStyle: {
                  color: '#fff',
                  fontSize: 7
                }
              }
            },
            coordinateSystem: 'geo', //使用坐标系
            //自定义散点图片
            symbolSize: 8,
            data: geoCoordMap
          }
        ]
      }
      // const seriesOption = {
      //   series: [
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       // 有波动
      //       name: '湖南省',
      //       value: [112.5439, 27.7075],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '湖北省',
      //       value: [114.3896, 30.6628], // 坐标值
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '福建省',
      //       value: [119.27345, 26.04769],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '广东省',
      //       value: [113.5107, 23.2196],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '贵州省',
      //       value: [109.26433, 27.81621],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '河北省',
      //       value: [115.45875, 38.87757],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '内蒙古',
      //       value: [111.62299, 40.80772],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     },
      //     {
      //       type: 'effectScatter',
      //       rippleEffect: {
      //         scale: 5,
      //         brushType: 'stroke'
      //       },
      //       name: '辽宁省',
      //       value: [123.1238, 42.1216],
      //       coordinateSystem: 'geo' // 表示在地图上显示散点图
      //     }
      //   ]
      // }
      this.chartInstance.setOption(initOption)
      // chartInstance.setOption(seriesOption)
    },
    async getData() {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      // const { data: ret } = await this.$http.get('map') http://101.43.184.159:8005/infoservice/activity/queryAll
      const { data: res } = await this.$http.get(
        'infoservice/activity/queryAll'
      )
      console.log(res.data.list)
      this.aclist = res.data.list
      this.allData = Map
      console.log(this.allData)
    },
    async map(newval, oldval) {
      let chartInstance = this.$ECharts.init(this.$refs.map_ref, 'chalk')
      console.log(chartInstance)
      console.log(newval, oldval)
      this.province = newval
      const provinceInfo = getProvinceMapInfo(newval)
      console.log(provinceInfo)
      // 需要获取这个省份的地图矢量数据
      // 判断当前所点击的这个省份的地图矢量数据在mapData中是否存在
      if (!this.mapData[provinceInfo.key]) {
        console.log(provinceInfo.path, '这是省份信息的path')
        const ret = await axios.get(
          'http://localhost:8080/' + provinceInfo.path
        )
        this.mapData[provinceInfo.key] = ret.data
        this.$ECharts.registerMap(provinceInfo.key, ret.data)
      }
      let geoCoordMap = [
        { name: '广东', value: [113.5107, 23.2196] },
        { name: '贵州', value: [109.26433, 27.81621] },
        { name: '湖南', value: [112.5439, 27.7075] },
        { name: '湖北', value: [114.3896, 30.6628] },
        { name: '福建', value: [119.27345, 26.04769] },
        { name: '河北', value: [115.45875, 38.87757] },
        { name: '内蒙古', value: [111.62299, 40.80772] },
        { name: '辽宁', value: [123.1238, 42.1216] }
      ]
      const changeOption = {
        title: {
          //text: '▎ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: provinceInfo.key,
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: 'rgba(255,255,255,0.2)',
            borderColor: '#333'
          },
          label: {
            normal: {
              //show: true,
              textStyle: {
                fontSize: 15,
                fontWeight: 'bold',
                color: '#fff'
              }
            }
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        },
        series: [
          {
            symbol: 'none',
            rippleEffect: {
              scale: 5,
              brushType: 'stroke'
            },
            tooltip: {
              trigger: 'item',
              show: false
            },
            // name: '省市',
            legend: {
              textStyle: {
                color: '#fff',
                fontSize: 7
              }
            },
            color: 'rgb(217, 142, 0)',
            type: 'effectScatter',
            label: {
              normal: {
                show: true,
                position: 'top',
                formatter: '{b}',
                textStyle: {
                  color: '#fff',
                  fontSize: 7
                }
              }
            },
            coordinateSystem: 'geo', //使用坐标系
            //自定义散点图片
            symbolSize: 8,
            data: geoCoordMap
          }
        ]
      }
      document.getElementById('toolbox').style.display = 'block'
      console.log(provinceInfo.key)
      chartInstance.setOption(changeOption)
    },
    updateChart() {
      // // 处理图表需要的数据
      // // 图例的数据
      // const legendArr = this.allData.map((item) => {
      //   return item.name
      // })
      // const seriesArr = this.allData.map((item) => {
      //   // return的这个对象就代表的是一个类别下的所有散点数据
      //   // 如果想在地图中显示散点的数据, 我们需要给散点的图表增加一个配置, coordinateSystem:geo
      //   return {
      //     type: 'effectScatter',
      //     rippleEffect: {
      //       scale: 5,
      //       brushType: 'stroke'
      //     },
      //     name: item.name,
      //     data: item.children,
      //     coordinateSystem: 'geo'
      //   }
      // })
      // const dataOption = {
      //   legend: {
      //     data: legendArr
      //   },
      //   series: seriesArr
      // }
      // this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      // const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      // const adapterOption = {
      //   title: {
      //     textStyle: {
      //       fontSize: titleFontSize
      //     }
      //   },
      //   legend: {
      //     itemWidth: titleFontSize / 2,
      //     itemHeight: titleFontSize / 2,
      //     itemGap: titleFontSize / 2,
      //     textStyle: {
      //       fontSize: titleFontSize / 2
      //     }
      //   }
      // }
      // this.chartInstance.setOption(adapterOption)
      // this.chartInstance.resize()
    },
    // 回到中国地图
    revertMap() {
      // const revertOption = {
      //   geo: {
      //     map: 'china'
      //   }
      // }
      // this.chartInstance.setOption(revertOption)
    }
  }
}
</script>
<style lang="less" scoped>
.acBox {
  display: flex;
  flex-direction: column;
  height: 44vh;
  margin: 10px 0;
  .acMap {
    position: relative;
    background-color: rgba(137, 2, 2, 0.32);
    flex: 8;
    margin-bottom: 7px;
    border: 1px solid rgb(255, 0, 14);
    &::before {
      position: absolute;
      top: 0;
      left: 0;
      content: '';
      width: 10px;
      height: 10px;
      border-left: 2px solid rgb(227, 93, 28);
      border-top: 2px solid rgb(255, 225, 123);
    }
    &::after {
      position: absolute;
      top: 0;
      right: 0;
      content: '';
      width: 10px;
      height: 10px;
      border-right: 2px solid rgb(255, 225, 123);
      border-top: 2px solid rgb(227, 93, 28);
    }
    .search {
      position: relative;
      margin-left: 10px;
      img {
        position: absolute;
        top: 9px;
        left: 15px;
        width: 10px;
        height: 10px;
      }
      input {
        background-color: transparent;
        color: #caa0a6;
        padding-left: 15px;
        border-radius: 5px;
        border: 1px solid #9e3d3d;
        outline: none; /*outline外边框，input:focus聚焦*/
        height: 30px;
        width: 160px;
        box-shadow: inset 0 1px 1px rgb(0 0 0 / 8%); //可以设置输入框阴影
      }
      input::-webkit-input-placeholder {
        color: rgba(255, 255, 255, 0.4);
        font-size: 12px;
      }
      input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: rgba(255, 255, 255, 0.4);
        font-size: 12px;
      }
      input:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: rgba(255, 255, 255, 0.4);
        font-size: 12px;
      }
      input:-ms-input-placeholder {
        /* Internet Explorer 10-11 */
        color: rgba(255, 255, 255, 0.4);
        font-size: 12px;
      }
    }
    .chinaMap {
      width: 100%;
      height: calc(100% - 32px);
    }
    #toolbox {
      position: absolute;
      top: 40px;
      left: 20px;
      background-color: #ffeb7b;
      opacity: 0.5;
      font-size: 16px;
      height: 120px;
      color: #000;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      padding-bottom: 10px;
      display: none;
    }
    .toolbox h3 {
      padding-top: 0.25em;
    }
    .introduction {
      text-align: center;
      height: 30px;
      padding: 0 10px;
      font: normal 400 14px '微软雅黑';
    }
  }
  .acList {
    position: fixed;
    right: 0;
    top: 0;
    height: 100vh;
    width: 33%;
    z-index: 999;
    background-color: rgba(137, 2, 2, 1);
    flex: 5;
    align-items: center;
    justify-content: center;
    margin-bottom: 2px;
    border: 1px solid rgb(255, 0, 14);
    &::before {
      position: absolute;
      top: 0;
      left: 0;
      content: '';
      width: 10px;
      height: 10px;
      border-left: 2px solid rgb(227, 93, 28);
      border-top: 2px solid rgb(255, 225, 123);
    }
    &::after {
      position: absolute;
      top: 0;
      right: 0;
      content: '';
      width: 10px;
      height: 10px;
      border-right: 2px solid rgb(255, 225, 123);
      border-top: 2px solid rgb(227, 93, 28);
    }
    .listFoot {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      &::before {
        position: absolute;
        right: 0;
        bottom: 0;
        content: '';
        width: 10px;
        height: 10px;
        border-right: 2px solid rgb(227, 93, 28);
        border-bottom: 2px solid rgb(255, 225, 123);
      }
      &::after {
        position: absolute;
        bottom: 0;
        left: 0;
        content: '';
        width: 10px;
        height: 10px;
        border-left: 2px solid rgb(255, 225, 123);
        border-bottom: 2px solid rgb(227, 93, 28);
      }
    }
    .listTitle {
      color: #fff;
      font-size: 0.375rem;
      font-weight: 900;
      font-family: monospace;
      height: 0.625rem;
      line-height: 0.625rem;
      padding-left: 0.25rem;
    }
    .listBox {
      margin: 5px 3px;
      display: flex;
      align-items: center;
      justify-content: center;
      table {
        border-collapse: collapse;
        width: 100%;
        text-align: center;
        thead {
          line-height: 25px;
          height: 30px;
          font-size: 10px;
          color: #fff;
          font-weight: 700;
          background-color: rgba(190, 53, 47, 0.8);
        }
        tbody {
          line-height: 25px;
          font-size: 8px;
          height: 25px;
          tr:nth-child(even) {
            background: rgba(193, 105, 92, 1);
            color: rgb(220, 236, 246);
          }
          tr:nth-child(odd) {
            background: rgba(131, 1, 1, 1);
            color: rgb(220, 166, 146);
          }
        }
      }
    }
  }
}
</style>
